#include "command.h"

void command_gets(int netFD, sendc_t *sc){
    int reply_num;
    recv(netFD, &reply_num, sizeof(reply_num), MSG_NOSIGNAL);
    if(reply_num != 1){
        printf("没有这个文件，请重新输入\n");
        return;
    }
    off_t file_size;
    recv(netFD, &file_size, sizeof(off_t), MSG_WAITALL);
    printf("%ld\n", file_size);

    char name[600] = { 0 };
    sprintf(name, "dir/%s", sc->path);

    int fd = open(name, O_RDWR|O_CREAT|O_TRUNC, 0600);

    ftruncate(fd, file_size);

    char *p = (char*)mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

    int rec = recv(netFD, p, file_size, MSG_WAITALL);
    printf("rec = %d\n", rec);

    munmap(p, file_size);
    close(fd);
    printf("下载成功\n");
}
